<template>
	<view class="wrap">
		<view class="item" v-for="(res, index) in addressTotal" :key="res.id">
			<view class="top" @tap="hangleId(res.id)">
				<view class="name">{{ res.name }}</view>
				<view class="phone">{{ (res.phone).replace(/^(\d{3})\d{4}(\d+)/,"$1****$2") }}</view>
				<!-- <view class="tag">
					<text v-for="(item, index) in res.tag" :key="index" :class="{red:item.tagText=='默认'}">{{ item.tagText }}</text>
				</view> -->
			</view>
			<view class="bottom">
				<view>{{res.province}}{{res.city}}{{res.county}}{{res.address}}</view>
				<u-icon name="edit-pen" :size="40" color="#999999" @tap="modifyAddress(res.id)"></u-icon>
				<u-icon name="trash-fill" :size="40" color="#999999" @tap="deleteAddress(res.id)"></u-icon>
			</view>
		</view>
		<view class="addSite" @tap="toAddSite">
			<view class="add">
				<u-icon name="plus" color="#ffffff" class="icon" :size="30"></u-icon>新建收货地址
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			siteList: [],
			addressTotal:[],
			addressId:'',
			sendAddressId:''
		};
	},
	onShow() {
		this.getData();
	},
	methods: {
		// 获取用户保存的地址信息
	    async getData() {
			await this.$u.get('/api/address').then(res=>{
				this.addressTotal=(res.data.data).reverse()
			}).catch(err=>{
				
			})
		},
		toAddSite(){
			uni.navigateTo({
			    url: '/pages/userInfoChange/adressEdit'
			});
		},
		modifyAddress(res){
			console.log(res)
			this.addressId=res
			uni.$on("send",res=>{
				uni.$emit('addressId',this.addressId)
			})
			// 跳转到修改地址界面
			uni.navigateTo({
			    url: '/pages/userInfoChange/modifyAddress'
			});
		},
		// 删除地址
	    async deleteAddress(res){
			this.addressId=res
			await this.$u.delete('/api/address/' +this.addressId).then(res=>{
				uni.showToast({
					title:'删除成功',
					icon:'success'
				})
				this.getData()
				// setInterval(res=>{
				// 	this.getData();
				// },1000)
				
			}).catch(err=>{
				console.log(err)
			})
			// clearInterval()
		},
		// 点击获取地址id,并传递个预览订单
		hangleId(addressId){
			this.sendAddressId=addressId
			uni.$on('send',res=>{
				uni.$emit('addressId',this.sendAddressId)
			});
			uni.redirectTo({
				url:'/pages/order-preview/order-preview'
			});
			// 并且缓存地址id,方便用户刷新页面数据丢失
			uni.setStorageSync('address',this.sendAddressId)
		}
	}
};
</script>

<style lang="scss" scoped>
.wrap{
	padding: 40rpx 40rpx 200rpx 40rpx;
}
.item {
	padding: 40rpx 20rpx;
	.top {
		display: flex;
		// justify-content: space-around;
		font-weight: bold;
		font-size: 34rpx;
		.phone {
			margin-left: 60rpx;
		}
		.tag {
			display: flex;
			font-weight: normal;
			align-items: center;
			text {
				display: block;
				width: 60rpx;
				height: 34rpx;
				line-height: 34rpx;
				color: #ffffff;
				font-size: 20rpx;
				border-radius: 6rpx;
				text-align: center;
				margin-left: 30rpx;
				background-color:rgb(49, 145, 253);
			}
			.red{
				background-color:red
			}
		}
	}
	.bottom {
		display: flex;
		margin-top: 20rpx;
		font-size: 28rpx;
		justify-content: space-between;
		color: #999999;
		view{
			flex: 7;
			overflow: hidden;
			text-overflow: ellipsis;
			white-space:nowrap;
		}
		.u-icon{
			flex: 1;
			padding-left: 10rpx;
		}
		
	}
}
.addSite {
	display: flex;
	justify-content: space-around;
	width: 600rpx;
	line-height: 100rpx;
	position: fixed;
	bottom: 0rpx;
	left: 80rpx;
	background-color: red;
	border-radius: 60rpx;
	font-size: 30rpx;
	.add{
		display: flex;
		align-items: center;
		color: #ffffff;
		.icon{
			margin-right: 10rpx;
		}
	}
}
</style>
